package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.base.util.AjaxResult;
import cn.itsource.pethome.base.util.PageInfo;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.query.ShopQuery;
import cn.itsource.pethome.org.service.IShopService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @PatchMapping//  相当于  @RequestMapping(value = "/shop",method = RequestMethod.PATCH)
    @ApiOperation(value = "查询所有的部门",notes = "我们这个接口有分页哦")
    public PageInfo<Shop> findPageByQuery(@RequestBody ShopQuery shopQuery){
        return shopService.findPageByQuery(shopQuery);
    }


    //批量删除
    @PatchMapping("/batchRemove")//  相当于  @RequestMapping(value = "/shop",method = RequestMethod.PATCH)
    @ApiOperation(value = "批量删除部门")
    public AjaxResult batchRemove(@RequestBody List<Shop> shops){
        try {
            shopService.batchRemove(shops);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }


    @GetMapping("/{id}")// 相当于      @RequestMapping(value = "/shop/{id}",method = RequestMethod.GET)
    public Shop findById(@PathVariable("id") Long id){
        return shopService.findById(id);
    }


    //新增和修改都走此方法
    /*param shop  如果id为空，就新增，如果id不为空就是修改
    @RequestBody :当你前端传递的参数是一个json对象的时候，并且后台想要接收对应的数
                 据，那后台必定要加此注解，否则接收不了前端传递的数据*/
    @PostMapping// 相当于    @RequestMapping(value = "/shop",method = RequestMethod.POST)
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult save(@RequestBody Shop shop){
        try {
            shopService.save(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }


    /*单个删除指定数据*/
    @DeleteMapping("/{id}")//相当于@RequestMapping(value = "/shop/{id}",method = RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /*商家入驻
    * shop里是店铺信息*/
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

}
